package com.regentsoft.cloudback.operateAdmin.controller;

import com.regentsoft.cloudback.base.BaseResp;
import com.regentsoft.cloudback.base.Pager;
import com.regentsoft.cloudback.base.ResponseCode;
import com.regentsoft.cloudback.operateAdmin.service.ActivationService;
import com.regentsoft.cloudback.operateAdmin.service.ActivationSubCodeService;
import com.regentsoft.cloudback.util.CodeUtil;
import com.regentsoft.cloudback.vo.QueryActivationVo;
import com.regentsoft.cloudback.vo.req.ActivationAddReq;
import com.regentsoft.cloudback.vo.req.ActivationSubCodeReq;
import com.regentsoft.cloudback.vo.req.CompanyActivationQueryReq;
import com.regentsoft.cloudback.vo.resp.CompanyActivationQueryResp;
import com.regentsoft.cloudback.vo.resp.CompanyIdQueryResp;
import com.regentsoft.cloudback.vo.resp.QueryActivationDetailResp;
import com.regentsoft.cloudback.vo.resp.QueryActivationResp;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author czd
 * @version 1.0
 * @date 2017年11月29日
 */
@RestController
@RequestMapping(value = "manager/activation/")
public class ActivationController {
    @Autowired
    private ActivationService service;
    @Autowired
    private ActivationSubCodeService subCodeService;

    @RequestMapping(value = "queryCompanyId", method = RequestMethod.GET)
    @ApiOperation(value = "模糊查询企业名字", httpMethod = "GET", response = CompanyIdQueryResp.class, notes = "模糊查询企业名字")
    private CompanyIdQueryResp queryCompanyId(@RequestParam String token, String name) {
        if (name == null)
            name = "";
        return new CompanyIdQueryResp(service.queryCompanyId(name));
    }


    @ApiOperation(value = "添加激活码", httpMethod = "POST", response = BaseResp.class, notes = "添加激活码")
    @RequestMapping(value = "addActivation", method = RequestMethod.POST)
    public BaseResp addActivation(@RequestParam String token, @RequestBody ActivationAddReq req) throws Exception {
        BaseResp resp = new BaseResp();

        if (req.getCode() == null || StringUtils.isEmpty(req.getCompanyId())||req.getCapacity()==0) {
            resp.setCode(ResponseCode.PARAMETERERR);
            resp.setMessage("填写数据不完整");
            return resp;
        }
        boolean flag = service.findByCode(req.getCode());
        if (flag&&req.getCodeId()==null) {
            resp.setCode(ResponseCode.FAIL);
            resp.setMessage("激活码已经存在");
            return resp;
        }
       
       service.addActivation(req.getCodeId(),req.getCode(), req.getCompanyId(), req.getCapacity(), req.getNote());
       resp.setCode(ResponseCode.SUCCESS);
       

        return resp;
    }

    @RequestMapping(value = "queryAllCompanyActivationCode", method = RequestMethod.GET)
    @ApiOperation(value = "查询企业激活码状态", httpMethod = "GET", response = CompanyIdQueryResp.class, notes = "查询企业激活码状态")
    public CompanyActivationQueryResp queryActivation(@RequestParam String token, CompanyActivationQueryReq queryReq) {
        return subCodeService.getSubCode(queryReq);
    }

    @RequestMapping(value = "changeSubCude", method = RequestMethod.GET)
    @ApiOperation(value = "改变激活码状态", httpMethod = "GET", response = CompanyIdQueryResp.class, notes = "改变激活码状态")
    public BaseResp changeSubCodeStatus(@RequestParam String token, ActivationSubCodeReq subCodeReq) {
        boolean result = subCodeService.changSubCodeStatus(subCodeReq);
        return new BaseResp(result);
    }

    @ApiOperation(value = "生成随机激活码", httpMethod = "GET", response = BaseResp.class, notes = "生成随机激活码")
    @RequestMapping(value = "randomActivation")
    public BaseResp randomActivation(@RequestParam String token) throws Exception {
        BaseResp resp = new BaseResp();
        String randomCode = CodeUtil.genCodes(6);
        boolean flag = false;
        while (true) {
            //检查激活码是否存在
            try {
				flag = service.findByCode(randomCode);
			} catch (Exception e) {
				throw new RuntimeException(e.getMessage(),e);
			}
            if (!flag) {
                break;
            }
            randomCode = CodeUtil.genCodes(6);
        }

        resp.setCode(ResponseCode.SUCCESS);
        resp.setMessage(randomCode);
        return resp;
    }

    @SuppressWarnings("unchecked")
    @ApiOperation(value = "查询激活码", httpMethod = "GET", response = QueryActivationResp.class, notes = "查询激活码")
    @RequestMapping(value = "queryActivation", method = RequestMethod.GET)
    public QueryActivationResp queryActivation(@RequestParam String token, @RequestParam Integer pageNumber, @RequestParam Integer pageSize) {
        QueryActivationResp resp = new QueryActivationResp();
        Pager pager = null;
        try {
            pager = service.findPageList(pageNumber, pageSize);
            List<QueryActivationVo> list = (List<QueryActivationVo>) pager.getDatas();
            if (list != null && list.size() > 0) {
                resp.setData(list);
                resp.setTotal(pager.getTotal());
            } else {
                resp.setCode(ResponseCode.NONE_DATAS);
                resp.setMessage("没有数据");
                return resp;
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(),e);
        }
        resp.setCode(ResponseCode.SUCCESS);
        return resp;
    }
    
    
    @ApiOperation(value = "查询激活码详情", httpMethod = "GET", response = QueryActivationResp.class, notes = "查询激活码详情")
    @RequestMapping(value = "queryActivationDetail", method = RequestMethod.GET)
    public QueryActivationDetailResp queryActivationDetail(@RequestParam String token, @RequestParam String code) {
    	QueryActivationDetailResp resp = new QueryActivationDetailResp();
        try {
            QueryActivationVo vo = service.queryActivationDetail(code);
            resp.setData(vo);
        } catch (Exception e) {
        	throw new RuntimeException(e.getMessage(),e);
        }
        resp.setCode(ResponseCode.SUCCESS);
        return resp;
    }
    
    @ApiOperation(value = "删除激活码", httpMethod = "GET", response = BaseResp.class, notes = "删除激活码")
    @RequestMapping(value = "deleteActivation", method = RequestMethod.GET)
    public BaseResp deleteActivation(@RequestParam String token, @RequestParam String code) throws Exception{
    	BaseResp resp = new BaseResp();
		boolean result = service.deleteActivation(code);
		if(result) {
			resp.setCode(ResponseCode.SUCCESS);
		}else {
			resp.setCode(ResponseCode.FAIL);
			resp.setMessage("该激活码已绑定机器码，不能删除！");
		}
        return resp;
    }
}
